<template>
	<view class="content">
		<view class="list">
			<view class="tishi">若您忘记了密码，可在此重新设置新密码。</view>
			<!-- 手机号输入 -->
			<view class="list-call">
				<image class="img" src="/static/shilu-login/1.png"></image>
				<input class="sl-input" type="number" v-model="phone" maxlength="11" placeholder="请输入手机号" />
			</view>
			<!-- 新密码输入 -->
			<view class="list-call">
				<image class="img" src="/static/shilu-login/2.png"></image>
				<input class="sl-input" type="text" v-model="password" maxlength="32" placeholder="请输入新密码"
					:password="!showPassword" />
				<image class="img" :src="showPassword?'/static/shilu-login/op.png':'/static/shilu-login/cl.png'" @tap="display">
				</image>
			</view>
			<!-- 再次输密码 -->
			<view class="list-call">
				<image class="img" src="/static/shilu-login/2.png"></image>
				<input class="sl-input" type="text" v-model="newpassword" maxlength="32" placeholder="请再次输入新密码"
					:password="!newshowPassword" />
				<image class="img" :src="newshowPassword?'/static/shilu-login/op.png':'/static/shilu-login/cl.png'"
					@tap="newdisplay"></image>
			</view>
			<!-- 获取验证码 -->
			<view class="list-call">
				<image class="img" src="/static/shilu-login/3.png"></image>
				<input class="sl-input" type="number" v-model="code" maxlength="6" placeholder="验证码" />
				<button @click="getcode" :disabled="isSending" class="yzm">
					{{ isSending ? countdown + '秒后重试' : '发送验证码' }}
				</button>
			</view>
		</view>
		<view class="button-login" hover-class="button-hover" @tap="bindLogin()">
			<text>修改密码</text>
		</view>

	</view>
</template>

<script>
import {
		CodeMessage,	
		updatePassword
	} from "@/api/user.js"
	export default {
		data() {
			return {
				isSending:false,
				countdown:60,
				phone: '',
				second: 0,
				code: "",
				showPassword: false,
				newshowPassword: false,
				password: '',
				newpassword: '',
			}
		},

		methods: {
			startCountdown() {
				this.timer = setInterval(() => {
					if (this.countdown > 0) {
						this.countdown--;
				
					} else {
						this.stopCountdown();
					}
				}, 1000);
			},
			
			// 停止倒计时
			stopCountdown() {
				clearInterval(this.timer);
				this.timer = null;
				this.isSending = false;
			},
			display() {
				this.showPassword = !this.showPassword
			},
			newdisplay() {
				this.newshowPassword = !this.newshowPassword
			},
			
			getcode() {
				const phone = /^1[3-9]\d{9}$/;
				if (!phone.test(this.phone)) {
					this.$u.toast('请输入有效的手机号');
					return ;
				}
				this.isSending = true;
				this.countdown = 60; // 重置倒计时
				var data={number:this.phone}
				CodeMessage(data).then(res => {
					if (res.message == "success") {
						uni.showToast({
							type: 'success',
							title: '发送成功',
							icon: 'none',
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
						});
					}
				}).catch(err => {					
				})
				this.startCountdown();		
			},
			//手机号校验
			bindLogin() {
				if (!/^1[3-9]\d{9}$/.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号格式不正确'
					});
					return;
				}
				
		  // 密码格式验证：密码必须包含字母和数字，且长度6-32位
		  const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[A-Za-z\d]{6,32}$/;
		  if (!passwordRegex.test(this.password)) {
		    uni.showToast({
		      icon: 'none',
		      title: '密码应包含字母和数字，且长度6-32位'
		    });
		    return;
		  }
				// 校验两次密码是否一致
				if (this.password !== this.newpassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none'
					});
					return;
				}

				if (this.code.length < 1) {
					uni.showToast({
						icon: 'none',
						title: '请输入验证码'
					});
					return;
				}
				var data={phone:this.phone,password:this.password,code:this.code}
				updatePassword(data).then(res=>{
					if(res.message=="false"){
						uni.showToast({
							type:"error",
							icon: 'none',
							title: '验证码已过期'
						});
					}else if(res.message=="false1"){
						uni.showToast({
							type:"error",
							icon: 'none',
							title: '验证码错误'
						});
					}
					else if(res.message=="false2"){
						uni.showToast({
							type:"error",
							icon: 'none',
							title: '修改失败'
						});
					}else if(res.message=="success"){
						uni.showToast({
							type:"success",
							icon: 'none',
							title: '修改成功'
						});
					}
				})

			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.tishi {
		color: #999999;
		font-size: 28rpx;
		line-height: 50rpx;
		margin-bottom: 50rpx;
	}

	.list {
		display: flex;
		flex-direction: column;
		padding-top: 50rpx;
		padding-left: 70rpx;
		padding-right: 70rpx;
	}

	.list-call {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
		color: #333333;
		border-bottom: 0.5px solid #e2e2e2;
	}

	.list-call .img {
		width: 40rpx;
		height: 40rpx;
	}

	.list-call .sl-input {
		flex: 1;
		text-align: left;
		font-size: 32rpx;
		margin-left: 16rpx;
	}

	.button-login {
		color: #FFFFFF;
		font-size: 34rpx;
		width: 470rpx;
		height: 100rpx;
		background: linear-gradient(-90deg, rgba(63, 205, 235, 1), rgba(188, 226, 158, 1));
		box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.2);
		border-radius: 50rpx;
		line-height: 100rpx;
		text-align: center;
		margin-left: auto;
		margin-right: auto;
		margin-top: 100rpx;
	}

	.button-hover {
		background: linear-gradient(-90deg, rgba(63, 205, 235, 0.8), rgba(188, 226, 158, 0.8));
	}

	.yzm {
		color: #FF7D13;
		font-size: 24rpx;
		line-height: 64rpx;
		padding-left: 10rpx;
		padding-right: 10rpx;
		width: auto;
		height: 64rpx;
		border: 1rpx solid rgba(255, 131, 30, 1);
		border-radius: 50rpx;
	}

	.yzms {
		color: #999999 !important;
		border: 1rpx solid #999999;
	}
</style>